<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改api服务')" />
	<th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-api-edit" th:object="${api}">
        	<input type="hidden" id="apiId" name="apiId" th:value="${api.apiId}">
        	<h4 class="form-header h4">基本设置</h4>
			<div class="form-group">	
				<label class="col-sm-3 control-label"><span style="color: red; ">*</span>api名称：</label>
				<div class="col-sm-8">
					<input id="apiName" name="apiName" class="form-control" type="text" required th:field="*{apiName}">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">api分组：</label>
				<div class="col-sm-8">
					<select id="apiGroupId" name="apiGroupId" class="form-control">
						<option value="" th:selected="${api.apiGroupId==null}">默认分组</option>
						<option th:selected="${api.apiGroupId==group.apiGroupId}" th:each="group:${groups}" th:value="${group.apiGroupId}" th:text="${group.apiGroupName}">自动检查</option>
					</select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<textarea id="remark" name="remark" class="form-control"></textarea>
				</div>
			</div>
			
			<h4 class="form-header h4">地址设置</h4>
			<div class="form-group">	
				<label class="col-sm-3 control-label">api地址后缀：</label>
				<div class="col-sm-8" style="display: flex;">
					<div class="form-control" th:text="${domian+gateurl}"></div><input id="apiUrlSuffix" name="apiUrlSuffix" class="form-control" type="text" placeholder="以'/'开头" required th:field="*{apiUrlSuffix}">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">转发地址后缀：</label>
				<div class="col-sm-8" style="display: flex;">
					<div class="form-control" id="apiServerUrlSuffixPre">[后端服务地址]</div><input id="apiServerUrlSuffix" name="apiServerUrlSuffix" class="form-control" type="text" placeholder="以'/'开头" required th:field="*{apiServerUrlSuffix}">
				</div>
			</div>
			<h4 class="form-header h4">服务端设置</h4>
			<div class="form-group">	
				<label class="col-sm-3 control-label"><span style="color: red; ">*</span>后端服务选择：</label>
				<div class="col-sm-8">
					<select id="apiServerIds" name="apiServerIds" class="form-control" required multiple="multiple">
						<optgroup th:label="${group.key.serverGroupName}" th:each="group : ${allGroups}">
							<option th:title="${server.serverUrl}" th:value="${server.serverId}" th:text="${server.serverName}" th:each="server : ${group.value}" th:attr ="selected=${serverIds.contains(server.serverId)?true:false}"></option>
						</optgroup>
					</select>
				</div>
			</div>
			
			<div class="form-group">	
				<label class="col-sm-3 control-label"><span style="color: red; ">*</span>负载均衡方式(多服务器时)：</label>
				<div class="col-sm-8">
					<select id="apiServerRequestWay" name="apiServerRequestWay" class="form-control" required>
						<option value="weight_round_robin" th:selected="*{apiServerRequestWay=='weight_round_robin'}">加权轮询</option>
					</select>
				</div>
			</div>
		</form>
	</div>
    <div th:include="include::footer"></div>
    <th:block th:include="include :: select2-js" />
    
    <script th:inline="javascript">
		var prefix = ctx + "api/apiUrl"
		$("#form-api-edit").validate({
			rules:{
				apiName:{
					required:true,
					remote: {
                        url: prefix + "/checkNameUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                        	apiName : function() {
                                return $.common.trim($("#apiName").val());
                            },
                            apiId : function() {
                                return $.common.trim($("#apiId").val());
                            }
                        },
                        dataFilter: function(data, type) {
                        	return $.validate.unique(data);
                        }
                    }
				},
			},
			focusCleanup: true
		});
		
		$("#apiServerIds").select2({
	        placeholder:'请选择',//默认文字提示
	        language: "zh-CN",
	        allowClear: true//允许清空
	    })
	    
		function submitHandler() {
			var apiServerIds = $("#apiServerIds").val();
			if(apiServerIds==null || apiServerIds==''){
				layer.msg("请选择服务器！",{icon:2});
			}else if($.validate.form()) {
	            $.operate.save(prefix + "/edit", $('#form-api-edit').serialize());
	        }
	    }
	</script>
</body>
</html>
